﻿using System;
using System.Windows;
using System.Windows.Input;

namespace ToDoListReminder.ViewModel
{
	/// <summary>
	/// Extends ICommand interface for work with Model View.
	/// </summary>
	internal class CommandProxy : Freezable, ICommand
	{
		/// <summary>
		/// Initializes a new instance of the CommandProxy class.
		/// </summary>
		/// <returns></returns>
		protected override Freezable CreateInstanceCore()
		{
			return new CommandProxy();
		}

		/// <summary>
		/// Registers the CommandProxy interface.
		/// </summary>
		public static readonly DependencyProperty CommandProperty =
			DependencyProperty.Register("Command",
				typeof(ICommand),
				typeof(CommandProxy),
				new UIPropertyMetadata());

		/// <summary>
		/// Gets or sets the command.
		/// </summary>
		public ICommand Command
		{
			get
			{
				return (ICommand)GetValue(CommandProperty);
			}
			set
			{
				SetValue(CommandProperty, value);
			}
		}

		/// <summary>
		/// Is calling when a caller wants to execute a command.
		/// </summary>
		/// <param name="parameter">A command parameter.</param>
		public void Execute(object parameter)
		{
			Command.Execute(parameter);
		}

		/// <summary>
		/// Is calling when a caller wants to know, if a command may be executed.
		/// </summary>
		/// <param name="parameter">A command parameter.</param>
		public bool CanExecute(object parameter)
		{
			return Command != null && Command.CanExecute(parameter);
		}

		/// <summary>
		/// Refelects adding or deleting an event handler.
		/// </summary>
		public event EventHandler CanExecuteChanged
		{
			add
			{
				CommandManager.RequerySuggested += value;
			}
			remove
			{
				CommandManager.RequerySuggested -= value;
			}
		}
	}

}
